home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / misc.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  4KB  |  250 lines

  1. /* Miscellaneous machine independent utilities
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "global.h"
  7. #include "socket.h"
  8. #include "mbuf.h"
  9.  
  10. /* convert a tcp port number or name to integer - WG7J */
  11. int
  12. atoip(char *s) {
  13.     int p,n;
  14.  
  15.     if((p=atoi(s)) == 0) {
  16.         n = strlen(s);
  17.         if(!strncmp(s,"convers",(size_t)n))
  18.             p = IPPORT_CONVERS;
  19.         else if(!strncmp(s,"telnet",(size_t)n))
  20.             p = IPPORT_TELNET;
  21.         else if(!strncmp(s,"ttylink",(size_t)n))
  22.             p = IPPORT_TTYLINK;
  23.     }
  24.     return p;
  25. }
  26.  
  27. /* Select from an array of strings, or return ascii number if out of range */
  28. char *
  29. smsg(msgs,nmsgs,n)
  30. char *msgs[];
  31. unsigned nmsgs,n;
  32. {
  33.     static char buf[16];
  34.  
  35.     if(n < nmsgs && msgs[n] != NULLCHAR)
  36.         return msgs[n];
  37.     sprintf(buf,"%u",n);
  38.     return buf;
  39. }
  40.  
  41. /* Convert hex-ascii to integer */
  42. int
  43. htoi(s)
  44. char *s;
  45. {
  46.     int i = 0;
  47.     char c;
  48.  
  49.     while((c = *s++) != '\0'){
  50.         if(c == 'x')
  51.             continue;    /* allow 0x notation */
  52.         if('0' <= c && c <= '9')
  53.             i = (i * 16) + (c - '0');
  54.         else if('a' <= c && c <= 'f')
  55.             i = (i * 16) + (c - 'a' + 10);
  56.         else if('A' <= c && c <= 'F')
  57.             i = (i * 16) + (c - 'A' + 10);
  58.         else
  59.             break;
  60.     }
  61.     return i;
  62. }
  63. /* replace terminating end of line marker(s) with null */
  64. void
  65. rip(s)
  66. register char *s;
  67. {
  68.     register char *cp;
  69.  
  70.     if((cp = strchr(s,'\n')) != NULLCHAR)
  71.         *cp = '\0';
  72. }
  73.  
  74. /* Copy a string to a malloc'ed buffer. Turbo C has this one in its
  75.  * library, but it doesn't call mallocw() and can therefore return NULL.
  76.  * NOS uses of strdup() generally don't check for NULL, so they need this one.
  77.  */
  78. char *
  79. strdup(s)
  80. const char *s;
  81. {
  82.     register char *out;
  83.     register int len;
  84.  
  85.     if(s == NULLCHAR)
  86.         return NULLCHAR;
  87.     len = strlen(s);
  88.     out = mallocw((unsigned)len+1);
  89.     /* This is probably a tad faster than strcpy, since we know the len */
  90.     memcpy(out,s,(size_t)len);
  91.     out[len] = '\0';
  92.     return out;
  93. }
  94. /* Routines not needed for Turbo 2.0, but available for older libraries */
  95. #ifdef    AZTEC
  96.  
  97. /* Case-insensitive string comparison */
  98. strnicmp(a,b,n)
  99. register char *a,*b;
  100. register int n;
  101. {
  102.     char a1,b1;
  103.  
  104.     while(n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0'){
  105.         if(a1 == b1)
  106.             continue;    /* No need to convert */
  107.         a1 = tolower(a1);
  108.         b1 = tolower(b1);
  109.         if(a1 == b1)
  110.             continue;    /* NOW they match! */
  111.         if(a1 > b1)
  112.             return 1;
  113.         if(a1 < b1)
  114.             return -1;
  115.     }
  116.     return 0;
  117. }
  118.  
  119. char *
  120. strtok(s1,s2)
  121. char *s1;    /* Source string (first call) or NULL */
  122. #ifdef    __STDC__    /* Ugly kludge for aztec's declaration */
  123. const char *s2;    /* Delimiter string */
  124. #else
  125. char *s2;    /* Delimiter string */
  126. #endif
  127. {
  128.     static int isdelim();
  129.     static char *next;
  130.     register char *cp;
  131.     char *tmp;
  132.  
  133.     if(s2 == NULLCHAR)
  134.         return NULLCHAR;    /* Must give delimiter string */
  135.  
  136.     if(s1 != NULLCHAR)
  137.         next = s1;        /* First call */
  138.  
  139.     if(next == NULLCHAR)
  140.         return NULLCHAR;    /* No more */
  141.  
  142.     /* Find beginning of this token */
  143.     for(cp = next;*cp != '\0' && isdelim(*cp,s2);cp++)
  144.         ;
  145.  
  146.     if(*cp == '\0')
  147.         return NULLCHAR;    /* Trailing delimiters, no token */
  148.  
  149.     /* Save the beginning of this token, and find its end */
  150.     tmp = cp;
  151.     next = NULLCHAR;    /* In case we don't find another delim */
  152.     for(;*cp != '\0';cp++){
  153.         if(isdelim(*cp,s2)){
  154.             *cp = '\0';
  155.             next = cp + 1;    /* Next call will begin here */
  156.             break;
  157.         }
  158.     }
  159.     return tmp;
  160. }
  161. static int
  162. isdelim(c,delim)
  163. char c;
  164. register char *delim;
  165. {
  166.     char d;
  167.  
  168.     while((d = *delim++) != '\0'){
  169.         if(c == d)
  170.             return 1;
  171.     }
  172.     return 0;
  173. }
  174. #endif    /* AZTEC */
  175.  
  176.  
  177.  
  178. /* Host-network conversion routines, replaced on the x86 with
  179.  * assembler code in pcgen.asm
  180.  */
  181. #ifndef    MSDOS
  182. /* Put a long in host order into a char array in network order */
  183. char *
  184. put32(cp,x)
  185. register char *cp;
  186. int32 x;
  187. {
  188.     *cp++ = x >> 24;
  189.     *cp++ = x >> 16;
  190.     *cp++ = x >> 8;
  191.     *cp++ = x;
  192.     return cp;
  193. }
  194. /* Put a short in host order into a char array in network order */
  195. char *
  196. put16(cp,x)
  197. register char *cp;
  198. int16 x;
  199. {
  200.     *cp++ = x >> 8;
  201.     *cp++ = x;
  202.  
  203.     return cp;
  204. }
  205. int16
  206. get16(cp)
  207. register char *cp;
  208. {
  209.     register int16 x;
  210.  
  211.     x = uchar(*cp++);
  212.     x <<= 8;
  213.     x |= uchar(*cp);
  214.     return x;
  215. }
  216. /* Machine-independent, alignment insensitive network-to-host long conversion */
  217. int32
  218. get32(cp)
  219. register char *cp;
  220. {
  221.     int32 rval;
  222.  
  223.     rval = uchar(*cp++);
  224.     rval <<= 8;
  225.     rval |= uchar(*cp++);
  226.     rval <<= 8;
  227.     rval |= uchar(*cp++);
  228.     rval <<= 8;
  229.     rval |= uchar(*cp);
  230.  
  231.     return rval;
  232. }
  233. /* Compute int(log2(x)) */
  234. int
  235. log2(x)
  236. register int16 x;
  237. {
  238.     register int n = 16;
  239.     for(;n != 0;n--){
  240.         if(x & 0x8000)
  241.             break;
  242.         x <<= 1;
  243.     }
  244.     n--;
  245.     return n;
  246. }
  247.  
  248. #endif
  249.  
  250.